#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFile>
#include <qqtdatapersistence.h>

#include <qqtlongdataserialization.h>

#include <qqtlongdataprotocol.h>

MainWindow::MainWindow ( QWidget* parent ) :
    QMainWindow ( parent ),
    ui ( new Ui::MainWindow )
{
    ui->setupUi ( this );

    m0 = new QQtFindServerManager ( this );
    m0->setServerPort ( 6000 );
    m0->setLocalPort ( 6001 );
    connect ( m0, SIGNAL ( signalFindServer ( QString, int ) ),
              this, SLOT ( slotFindServer ( QString, int ) ) );


    c0 = new QQtUdpClient ( this );
    p0 = new QQtLongDataProtocol ( this );
    c0->installProtocol ( p0 );

    c0->bind ( 6002, QQtUdpClient::DefaultForPlatform );
    m0->sendFindServer();

    m1 = new QQtLongDataManager ( this );
    m1->setLocalPort ( 6003 );


    m_timer = new QTimer ( this );
    m_timer->setSingleShot ( false );
    //m_timer->setInterval ( 10 );
    connect ( m_timer, SIGNAL ( timeout() ),
              this, SLOT ( slotTimeout() ) );

    dp0.setDataFile ( "source.json" );
    dp0.prepareDataPersistence();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slotTimeout()
{
    QByteArray bytes;

    dp0.start();
    QQtDictionary& handler = dp0.dictionary();
    bytes = handler.toJson ( QJsonDocument::Indented );
    dp0.stop();

    QQtLongDataSerialization ds0;
    ds0[0] = "json_2";
    ds0[1] = bytes;
    ds0.serialize();

    static int i = 0;
    p3line() << i++;
    p3line() << ds0[0].getValue().toString();
    p3line() << ds0[1].getValue().toByteArray().size();

    static int size = 0;
    ui->textBrowser->clear();
    ui->textBrowser->append ( QString::number ( i ) + ", "
                              + QString::number ( ds0[1].getValue().toByteArray().size() ) );
    ui->textBrowser->append ( QString::number ( size + ds0[1].getValue().toByteArray().size() ) );
    size += ds0[1].getValue().toByteArray().size();
    //p3line() << ds0[1];
    p0->sendMessage ( ds0 );
}

void MainWindow::slotFindServer ( QString ip, int port )
{
    c0->setServerAddress ( QHostAddress ( ip ), port );
    m1->setServerAddress ( QHostAddress ( ip ), port );
}

void MainWindow::on_pushButton_clicked ( bool checked )
{
    m0->sendFindServer();
}

void MainWindow::on_pushButton_2_clicked ( bool checked )
{

    QFile file ( "source.json" );
    file.open ( QFile::ReadOnly );
    QByteArray bytes = file.readAll();
    file.close();

    QQtDictionary dict;
    dict.fromJson ( bytes );

    QQtLongDataSerialization ds0;
    ds0[0] = "json";
    ds0[1] = dict.toJson ( QJsonDocument::Compact );
    ds0.serialize();
    p3line() << ds0[0].getValue().toString();
    p3line() << ds0[1].getValue().toByteArray().size();
    p3line() << ds0[1];

    //ds0.unserialize();
    //ds0.dump();

    ui->textBrowser->clear();
    ui->textBrowser->append ( QString::number ( ds0[1].getValue().toByteArray().size() ) );
    ui->textBrowser->append ( dict.toJson ( QJsonDocument::Indented ) );

    p0->sendMessage ( ds0 );
}

void MainWindow::on_pushButton_3_clicked ( bool checked )
{
    //return;

    //必须手动转换转换成base64，只要有了base64就可以把二进制数据转换到字符串从json data发送出去。

    QFile file ( "aaa.png" );
    file.open ( QFile::ReadOnly );
    QByteArray bytes = file.readAll();
    file.close();

    bytes = bytes.toBase64();

    QQtLongDataSerialization ds0;
    ds0[0] = "binary";
    ds0[1] = bytes;
    ds0.serialize();
    p3line() << ds0[0].getValue().toString();
    p3line() << ds0[1].getValue().toByteArray().size();
    //p3line() << ds0[1];
    p0->sendMessage ( ds0 );
}

void MainWindow::on_pushButton_4_clicked ( bool checked )
{
    QByteArray bytes;

    QQtLongDataSerialization ds0;
    ds0[0] = "empty";
    ds0[1] = bytes;
    ds0.serialize();
    p3line() << ds0[0].getValue().toString();
    p3line() << ds0[1].getValue().toByteArray().size();
    //p3line() << ds0[1];
    p0->sendMessage ( ds0 );
}

void MainWindow::on_pushButton_5_clicked ( bool checked )
{
    //return;
    //必须手动转换转换成base64，+压缩

    QFile file ( "image.zip" );
    file.open ( QFile::ReadOnly );
    QByteArray bytes = file.readAll();
    file.close();

    //压缩真有用吗？
    bytes = qCompress ( bytes );
    bytes = bytes.toBase64();

    QQtLongDataSerialization ds0;
    ds0[0] = "2m";
    ds0[1] = bytes;
    ds0.serialize();
    p3line() << ds0[0].getValue().toString();
    p3line() << ds0[1].getValue().toByteArray().size();
    //p3line() << ds0[1];
    p0->sendMessage ( ds0 );
}

void MainWindow::on_pushButton_6_clicked ( bool checked )
{
    m_timer->start();
}

void MainWindow::on_pushButton_7_clicked ( bool checked )
{
    m_timer->stop();
}

void MainWindow::on_pushButton_8_clicked ( bool checked )
{
    QByteArray bytes;

    dp0.start();
    QQtDictionary& handler = dp0.dictionary();
    bytes = handler.toJson ( QJsonDocument::Indented );
    dp0.stop();

    QQtLongDataSerialization ds0;
    ds0[0] = "json";
    ds0[1] = bytes;
    ds0.serialize();

    static int i = 0;
    p3line() << i++;
    p3line() << ds0[0].getValue().toString();
    p3line() << ds0[1].getValue().toByteArray().size();

    ui->textBrowser->clear();
    QQtDictionary dict;
    dict.fromJson ( bytes );
    ui->textBrowser->append ( QString::number ( i ) + ", "
                              + QString::number ( ds0[1].getValue().toByteArray().size() ) );
    ui->textBrowser->append ( dict.toJson ( QJsonDocument::Indented ) );
    //p3line() << ds0[1];
    m1->sendMessage ( ds0 );
}
